{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f807009-b86e-4c7b-83d2-cc4ca178499e",
   "metadata": {},
   "source": [
    "## 2、Numpy简介\n",
    "### 1. Numpy的数组中，元素数据类型必须统一\n",
    "    * 要么都是整型，要么都是浮点型，要么都是字符串型，不能同时出现多种数据\n",
    "### 2. Numpy数组创建的4种方式\n",
    "    * np.array创建\n",
    "    * np.random创建\n",
    "    * np.arange创建\n",
    "    * np特殊函数创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "704b86c6-49c7-4ba8-a0c8-8e570c6413b2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T18:25:06.352630Z",
     "iopub.status.busy": "2022-01-24T18:25:06.352336Z",
     "iopub.status.idle": "2022-01-24T18:25:06.464352Z",
     "shell.execute_reply": "2022-01-24T18:25:06.463725Z",
     "shell.execute_reply.started": "2022-01-24T18:25:06.352562Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "10c012c6-b7a1-4f06-a44b-941e74a788d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T18:30:12.984958Z",
     "iopub.status.busy": "2022-01-24T18:30:12.984704Z",
     "iopub.status.idle": "2022-01-24T18:30:12.990386Z",
     "shell.execute_reply": "2022-01-24T18:30:12.989173Z",
     "shell.execute_reply.started": "2022-01-24T18:30:12.984931Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# 1 使用np.array从list创建数组\n",
    "a = np.array([1, 2, 3, 4])\n",
    "print(a)\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "407fde02-fcbc-44f8-9fe0-f5a8ded81ebb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T18:31:40.471564Z",
     "iopub.status.busy": "2022-01-24T18:31:40.471200Z",
     "iopub.status.idle": "2022-01-24T18:31:40.476897Z",
     "shell.execute_reply": "2022-01-24T18:31:40.476135Z",
     "shell.execute_reply.started": "2022-01-24T18:31:40.471536Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function array in module numpy:\n",
      "\n",
      "array(...)\n",
      "    array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,\n",
      "          like=None)\n",
      "    \n",
      "    Create an array.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    object : array_like\n",
      "        An array, any object exposing the array interface, an object whose\n",
      "        __array__ method returns an array, or any (nested) sequence.\n",
      "        If object is a scalar, a 0-dimensional array containing object is\n",
      "        returned.\n",
      "    dtype : data-type, optional\n",
      "        The desired data-type for the array.  If not given, then the type will\n",
      "        be determined as the minimum type required to hold the objects in the\n",
      "        sequence.\n",
      "    copy : bool, optional\n",
      "        If true (default), then the object is copied.  Otherwise, a copy will\n",
      "        only be made if __array__ returns a copy, if obj is a nested sequence,\n",
      "        or if a copy is needed to satisfy any of the other requirements\n",
      "        (`dtype`, `order`, etc.).\n",
      "    order : {'K', 'A', 'C', 'F'}, optional\n",
      "        Specify the memory layout of the array. If object is not an array, the\n",
      "        newly created array will be in C order (row major) unless 'F' is\n",
      "        specified, in which case it will be in Fortran order (column major).\n",
      "        If object is an array the following holds.\n",
      "    \n",
      "        ===== ========= ===================================================\n",
      "        order  no copy                     copy=True\n",
      "        ===== ========= ===================================================\n",
      "        'K'   unchanged F & C order preserved, otherwise most similar order\n",
      "        'A'   unchanged F order if input is F and not C, otherwise C order\n",
      "        'C'   C order   C order\n",
      "        'F'   F order   F order\n",
      "        ===== ========= ===================================================\n",
      "    \n",
      "        When ``copy=False`` and a copy is made for other reasons, the result is\n",
      "        the same as if ``copy=True``, with some exceptions for 'A', see the\n",
      "        Notes section. The default order is 'K'.\n",
      "    subok : bool, optional\n",
      "        If True, then sub-classes will be passed-through, otherwise\n",
      "        the returned array will be forced to be a base-class array (default).\n",
      "    ndmin : int, optional\n",
      "        Specifies the minimum number of dimensions that the resulting\n",
      "        array should have.  Ones will be pre-pended to the shape as\n",
      "        needed to meet this requirement.\n",
      "    like : array_like\n",
      "        Reference object to allow the creation of arrays which are not\n",
      "        NumPy arrays. If an array-like passed in as ``like`` supports\n",
      "        the ``__array_function__`` protocol, the result will be defined\n",
      "        by it. In this case, it ensures the creation of an array object\n",
      "        compatible with that passed in via this argument.\n",
      "    \n",
      "        .. versionadded:: 1.20.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray\n",
      "        An array object satisfying the specified requirements.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    empty_like : Return an empty array with shape and type of input.\n",
      "    ones_like : Return an array of ones with shape and type of input.\n",
      "    zeros_like : Return an array of zeros with shape and type of input.\n",
      "    full_like : Return a new array with shape of input filled with value.\n",
      "    empty : Return a new uninitialized array.\n",
      "    ones : Return a new array setting values to one.\n",
      "    zeros : Return a new array setting values to zero.\n",
      "    full : Return a new array of given shape filled with value.\n",
      "    \n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    When order is 'A' and `object` is an array in neither 'C' nor 'F' order,\n",
      "    and a copy is forced by a change in dtype, then the order of the result is\n",
      "    not necessarily 'C' as expected. This is likely a bug.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.array([1, 2, 3])\n",
      "    array([1, 2, 3])\n",
      "    \n",
      "    Upcasting:\n",
      "    \n",
      "    >>> np.array([1, 2, 3.0])\n",
      "    array([ 1.,  2.,  3.])\n",
      "    \n",
      "    More than one dimension:\n",
      "    \n",
      "    >>> np.array([[1, 2], [3, 4]])\n",
      "    array([[1, 2],\n",
      "           [3, 4]])\n",
      "    \n",
      "    Minimum dimensions 2:\n",
      "    \n",
      "    >>> np.array([1, 2, 3], ndmin=2)\n",
      "    array([[1, 2, 3]])\n",
      "    \n",
      "    Type provided:\n",
      "    \n",
      "    >>> np.array([1, 2, 3], dtype=complex)\n",
      "    array([ 1.+0.j,  2.+0.j,  3.+0.j])\n",
      "    \n",
      "    Data-type consisting of more than one element:\n",
      "    \n",
      "    >>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])\n",
      "    >>> x['a']\n",
      "    array([1, 3])\n",
      "    \n",
      "    Creating an array from sub-classes:\n",
      "    \n",
      "    >>> np.array(np.mat('1 2; 3 4'))\n",
      "    array([[1, 2],\n",
      "           [3, 4]])\n",
      "    \n",
      "    >>> np.array(np.mat('1 2; 3 4'), subok=True)\n",
      "    matrix([[1, 2],\n",
      "            [3, 4]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4e7cac4e-7e9d-404d-a5ff-b80dc33327d7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T18:44:44.757450Z",
     "iopub.status.busy": "2022-01-24T18:44:44.757192Z",
     "iopub.status.idle": "2022-01-24T18:44:44.762736Z",
     "shell.execute_reply": "2022-01-24T18:44:44.761546Z",
     "shell.execute_reply.started": "2022-01-24T18:44:44.757423Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 2 使用np.arange创建数组\n",
    "b=np.arange(10)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b959341c-757e-4a86-afda-02690e32a3d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T18:46:03.595392Z",
     "iopub.status.busy": "2022-01-24T18:46:03.594884Z",
     "iopub.status.idle": "2022-01-24T18:46:03.601218Z",
     "shell.execute_reply": "2022-01-24T18:46:03.599981Z",
     "shell.execute_reply.started": "2022-01-24T18:46:03.595360Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function arange in module numpy:\n",
      "\n",
      "arange(...)\n",
      "    arange([start,] stop[, step,], dtype=None, *, like=None)\n",
      "    \n",
      "    Return evenly spaced values within a given interval.\n",
      "    \n",
      "    Values are generated within the half-open interval ``[start, stop)``\n",
      "    (in other words, the interval including `start` but excluding `stop`).\n",
      "    For integer arguments the function is equivalent to the Python built-in\n",
      "    `range` function, but returns an ndarray rather than a list.\n",
      "    \n",
      "    When using a non-integer step, such as 0.1, it is often better to use\n",
      "    `numpy.linspace`. See the warnings section below for more information.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    start : integer or real, optional\n",
      "        Start of interval.  The interval includes this value.  The default\n",
      "        start value is 0.\n",
      "    stop : integer or real\n",
      "        End of interval.  The interval does not include this value, except\n",
      "        in some cases where `step` is not an integer and floating point\n",
      "        round-off affects the length of `out`.\n",
      "    step : integer or real, optional\n",
      "        Spacing between values.  For any output `out`, this is the distance\n",
      "        between two adjacent values, ``out[i+1] - out[i]``.  The default\n",
      "        step size is 1.  If `step` is specified as a position argument,\n",
      "        `start` must also be given.\n",
      "    dtype : dtype\n",
      "        The type of the output array.  If `dtype` is not given, infer the data\n",
      "        type from the other input arguments.\n",
      "    like : array_like\n",
      "        Reference object to allow the creation of arrays which are not\n",
      "        NumPy arrays. If an array-like passed in as ``like`` supports\n",
      "        the ``__array_function__`` protocol, the result will be defined\n",
      "        by it. In this case, it ensures the creation of an array object\n",
      "        compatible with that passed in via this argument.\n",
      "    \n",
      "        .. versionadded:: 1.20.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    arange : ndarray\n",
      "        Array of evenly spaced values.\n",
      "    \n",
      "        For floating point arguments, the length of the result is\n",
      "        ``ceil((stop - start)/step)``.  Because of floating point overflow,\n",
      "        this rule may result in the last element of `out` being greater\n",
      "        than `stop`.\n",
      "    \n",
      "    Warnings\n",
      "    --------\n",
      "    The length of the output might not be numerically stable.\n",
      "    \n",
      "    Another stability issue is due to the internal implementation of\n",
      "    `numpy.arange`.\n",
      "    The actual step value used to populate the array is\n",
      "    ``dtype(start + step) - dtype(start)`` and not `step`. Precision loss\n",
      "    can occur here, due to casting or due to using floating points when\n",
      "    `start` is much larger than `step`. This can lead to unexpected\n",
      "    behaviour. For example::\n",
      "    \n",
      "      >>> np.arange(0, 5, 0.5, dtype=int)\n",
      "      array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n",
      "      >>> np.arange(-3, 3, 0.5, dtype=int)\n",
      "      array([-3, -2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8])\n",
      "    \n",
      "    In such cases, the use of `numpy.linspace` should be preferred.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    numpy.linspace : Evenly spaced numbers with careful handling of endpoints.\n",
      "    numpy.ogrid: Arrays of evenly spaced numbers in N-dimensions.\n",
      "    numpy.mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.arange(3)\n",
      "    array([0, 1, 2])\n",
      "    >>> np.arange(3.0)\n",
      "    array([ 0.,  1.,  2.])\n",
      "    >>> np.arange(3,7)\n",
      "    array([3, 4, 5, 6])\n",
      "    >>> np.arange(3,7,2)\n",
      "    array([3, 5])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.arange)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f708c157-1a4d-4a46-a003-0f31d3540853",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:06:38.339006Z",
     "iopub.status.busy": "2022-01-24T19:06:38.338771Z",
     "iopub.status.idle": "2022-01-24T19:06:38.344421Z",
     "shell.execute_reply": "2022-01-24T19:06:38.343715Z",
     "shell.execute_reply.started": "2022-01-24T19:06:38.338982Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.80791478 0.16532928 0.12177298]\n",
      " [0.25832448 0.39922027 0.25780706]]\n",
      "[[4 4 0 7]\n",
      " [3 5 6 4]\n",
      " [0 8 8 5]\n",
      " [6 0 6 2]]\n"
     ]
    }
   ],
   "source": [
    "# 3 使用np.random创建数组\n",
    "## 3.1 np.random.random(N, M)创建一个N行M列的随机数组，每个元素都是0-1之间的小数\n",
    "c1 = np.random.random((2,3))\n",
    "print(c1)\n",
    "## 3.2 np.random.randint(i, j, size=(N, M))创建一个N行M列的随机数组，每个元素都是i-j之间的整数\n",
    "c2 = np.random.randint(0,9,size=(4,4))\n",
    "print(c2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3a9672db-cd5e-4357-a9b0-b3fd545ed82d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:05:58.164409Z",
     "iopub.status.busy": "2022-01-24T19:05:58.164161Z",
     "iopub.status.idle": "2022-01-24T19:05:58.172564Z",
     "shell.execute_reply": "2022-01-24T19:05:58.171421Z",
     "shell.execute_reply.started": "2022-01-24T19:05:58.164384Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0.]\n",
      " [0. 0.]\n",
      " [0. 0.]]\n",
      "[[1. 1.]\n",
      " [1. 1.]\n",
      " [1. 1.]]\n",
      "[[8 8]\n",
      " [8 8]\n",
      " [8 8]]\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 4 使用np创建特殊数组\n",
    "## 4.1 np.zeros((N, M))创建一个N行M列的随机数组，每个元素都是0\n",
    "e1=np.zeros((3, 2))\n",
    "print(e1)\n",
    "## 4.2 np.ones((N, M))创建一个N行M列的随机数组，每个元素都是1\n",
    "e2=np.ones((3, 2))\n",
    "print(e2)\n",
    "## 4.3 np.full((N, M), k)创建一个N行M列的随机数组，每个元素都是k\n",
    "e3=np.full((3, 2), 8)\n",
    "print(e3)\n",
    "## 4.4 np.eye(N)创建一个N阶单位矩阵\n",
    "e4=np.eye(3)\n",
    "print(e4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
